#include "Delay.h"
#include "timer.h"




uint32_t TIM_Ts_Get(void)
{
	return ((uint32_t)__HAL_TIM_GET_COUNTER(&htim6));
}


void TIM_Delay_Ms(uint32_t ms)
{
	static uint32_t	TickLast,TickMs,TickSum,TickCur;
	
	TickSum = 0;
	TickMs = (ms * MS_TICKS);
	TickLast = __HAL_TIM_GET_COUNTER(&htim6);
	
	while(true)
	{
		TickCur = __HAL_TIM_GET_COUNTER(&htim6);
		
		if(TickCur >= TickLast)
		{
			TickSum += TickCur - TickLast;
		}
		else  //over flow
		{
			TickSum += (0xFFFF - TickLast) + TickCur;
		}
		
		if(TickSum >= TickMs)
		{	
			return; 
		}
		
		TickLast = TickCur;
	}
}
















